<?php
namespace Sys\Controller;
use Common\Controller\BaseController;
class StudentController extends BaseController{
    public function index(){
        $m=M('students');
        $res=$m->page(I('get.p'),2)->select();
        $this->assign('stu',$res);
        $count = $m->count();
        $Page = new \Think\Page($count,2);
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $show = $Page->show();
        $this->assign('page',$show);
        $this->display();
    }
    public function addStudent(){
        $this->getDict('education');
        $this->getDict('sex');
        $this->display();
    }
    public function doAddStudent(){
        $info = $this->up();
        $bigImg = './UpLoads/'.$info['file']['savepath'].$info['file']['savename'];
        $mdImg = './UpLoads/'.$info['file']['savepath'].$info['file']['savename'].'md.'.$info['file']['ext'];
        $smImg = './UpLoads/'.$info['file']['savepath'].$info['file']['savename'].'sm.'.$info['file']['ext'];

        $this->thumb($bigImg,$mdImg,200);
        $this->thumb($bigImg,$smImg,100);

        $m = D('students');

        $_POST['lg_photo']=$bigImg;
        $_POST['md_photo']=$mdImg;
        $_POST['sm_photo']=$smImg;
       //dump($_POST);exit;

        if($m->create($_POST)){
            $m->add();
           header("location:./index");
        }else{
            echo $m->getError();
        }
    }

    public function up(){
        $up=new \Think\Upload();
        $up->maxsize=1*1024*1024;
        $up->exts=['jpg', 'gif', 'png', 'jpeg'];
        $up->rootPath='./UpLoads/';
        //$upload->savePath='';
        $info=$up->upload();
        if($info){
            return $info;
        }else{
            $this->error($up->getError());
        }
    }
    public function thumb($srcimg,$thumbimg,$w){
        $img = new \Think\Image();
        $img->open($srcimg);
        $img->thumb($w,$w,6)
            ->save($thumbimg);
    }
    public function doEditStudent(){
        $m=M('students');//D(')
        if($m->create()){
            $m->save();
            $res = [
                'msg'=>'编辑成功',
                'error'=>0,
                'data'=>I('post.')
            ];
        }else{
            $res = [
                'msg'=>"编辑失败",
                'error'=>1,

            ];
        }

        $this->ajaxReturn($res);

    }
    public function doDelStudent(){
        $id=I('post.ids');
        $this->unlinkphoto($id);
        $m=M('students');
        if($m->delete($id)){
            $res=[
                'msg'=>'删除成功',
                'error'=>0
            ];
        }else{
            $res=[
                'msg'=>'删除失败',
                'error'=>1
            ];
        }
        $this->ajaxReturn($res);

    }
    //删除图片
    public function unlinkphoto($id){
        $where=[
            'student_id'=>$id
        ];
        $res=M('students')->field('lg_photo,md_photo,sm_photo')->where($where)->find();
        foreach($res as $v){
            unlink($v);
        }
    }


}
